package com.creative.accountsdk;

import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.creative.accountsdk.net.Protocol;
import com.loopj.android.http.AsyncHttpClient;

import java.util.Locale;

/**
 * Created by yinjialiang on 15-6-18.
 */
public class DataCenter {

    public static String IMEI = null;
    /**当前系统语言*/
    public static String LOCAL = null;
    /**登录时使用的语言*/
    public static String LOGIN_LOCAL = null;

    public final static int VERSION_CODE = 4;

    public final static String LOCAL_CN = "zh_CN";
    public final static String LOCAL_EN = "en_US";

    private final static String TAG = "DataCenter";

    public static void init(Context context)
    {
        if (IMEI == null) {
            TelephonyManager tm = (TelephonyManager) context.getSystemService(Activity.TELEPHONY_SERVICE);
            String imei = tm.getDeviceId();
            if (imei != null) {
                DataCenter.IMEI = imei;
            } else {
                IMEI = "00000000";
            }
            Log.d(TAG, "IMEI = " + IMEI);
        }
        //locale
        {
            Locale locale = context.getResources().getConfiguration().locale;
            String language = locale.getLanguage();
            Log.d(TAG, "language = " + language);
            if(language.endsWith("zh"))
            {
                LOCAL = LOCAL_CN;
            }
            else
            {
                LOCAL = LOCAL_EN;
            }
        }
    }

    public static boolean isForeign()
    {
        if(LOGIN_LOCAL != null)
        {
            return LOGIN_LOCAL.equals(LOCAL_EN);
        }
        return LOCAL.equals(LOCAL_EN);
    }

    public static void switchThirdLogin()
    {
        if(isForeign())
        {
            LOGIN_LOCAL = LOCAL_CN;
        }
        else
        {
            LOGIN_LOCAL = LOCAL_EN;
        }
    }

    private static AsyncHttpClient httpClient;
    public static AsyncHttpClient getHttpClient() {
        if (httpClient == null) {
            httpClient = new AsyncHttpClient();
            httpClient.setTimeout(Protocol.TIMEOUT_CONNECTION);
            httpClient.setResponseTimeout(Protocol.TIMEOUT_SOCKET);
        }
        return httpClient;
    }
}
